package com.cmp.core;

import com.cmp.arena.Arena;
import static com.cmp.core.constants.InputUIDConstants.*;
import com.cmp.screens.CharacterSelectScreen;
import com.cmp.screens.GameScreen;
import com.cmp.screens.MainMenu;
import com.cmp.screens.StartScreen;
import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.input.KeyInput;
import com.jme3.input.MouseInput;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.MouseAxisTrigger;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.niftygui.NiftyJmeDisplay;
import com.jme3.renderer.RenderManager;
import de.lessvoid.nifty.Nifty;

/**
 * @author krummer
 */
public class Main extends SimpleApplication {

    public static SimpleApplication app;

    public static void main(String[] args) {
        app = new Main();
        app.start();
    }

    @Override
    public void simpleInitApp() {
        
        //disable jME cam.
        flyCam.setEnabled(false);
        
        //create core app states n stuff
        this.getStateManager().attach(new BulletAppState());
        this.getStateManager().attach(new Arena());
               
        //init GUI
        this.prepareScreens();
            
        //init keyMappings
        this.mapKeys();
        //TODO: add options menu for bindings
        
    }
    
    private void prepareScreens() {
        //init GUI
        NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(assetManager,
                inputManager, audioRenderer, guiViewPort);
        Nifty nifty = niftyDisplay.getNifty();
        
        //load xml files
        nifty.addXml("Interface/startScreen.xml");
        nifty.addXml("Interface/mainMenu.xml");
        nifty.addXml("Interface/characterSelectScreen.xml");
        nifty.addXml("Interface/gameScreen.xml");
        
        //start with startScreen
        nifty.gotoScreen("start");
        
        //create bindings
        StartScreen startScreen = (StartScreen) nifty.getScreen("start").getScreenController();
        MainMenu mainMenu = (MainMenu) nifty.getScreen("mainMenu").getScreenController();
        CharacterSelectScreen charSelectScreen = (CharacterSelectScreen) nifty.getScreen("characterSelect").getScreenController();
        GameScreen gameScreen = (GameScreen) nifty.getScreen("game").getScreenController();
        
        //add to app states
        stateManager.attach(startScreen);
        stateManager.attach(mainMenu);
        stateManager.attach(charSelectScreen);
        stateManager.attach(gameScreen);
        
        guiViewPort.addProcessor(niftyDisplay);
    }
    
    private void mapKeys() {
        inputManager.addMapping(UP_BUTTON_UID, new KeyTrigger(KeyInput.KEY_W),
                                      new KeyTrigger(KeyInput.KEY_UP));
        inputManager.addMapping(DOWN_BUTTON_UID, new KeyTrigger(KeyInput.KEY_S),
                                      new KeyTrigger(KeyInput.KEY_DOWN));
        inputManager.addMapping(LEFT_BUTTON_UID, new KeyTrigger(KeyInput.KEY_A),
                                      new KeyTrigger(KeyInput.KEY_LEFT));
        inputManager.addMapping(RIGHT_BUTTON_UID, new KeyTrigger(KeyInput.KEY_D),
                                      new KeyTrigger(KeyInput.KEY_RIGHT));
        inputManager.addMapping(JUMP_BUTTON_UID, new KeyTrigger(KeyInput.KEY_SPACE));
        
        inputManager.addMapping(ATTACK_BUTTON_UID, new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
        inputManager.addMapping(ABILITY_BUTTON_UID, new MouseButtonTrigger(MouseInput.BUTTON_RIGHT));
        
        inputManager.addMapping(VIEW_DIRECTION_UPDATE_UID, new MouseAxisTrigger(MouseInput.AXIS_X, true),
                                                        new MouseAxisTrigger(MouseInput.AXIS_X, false),
                                                        new MouseAxisTrigger(MouseInput.AXIS_Y, true),
                                                        new MouseAxisTrigger(MouseInput.AXIS_Y, false));
    }

    @Override
    public void simpleUpdate(float tpf) {
        //TODO: add update code
        stateManager.update(tpf);
    }

    @Override
    public void simpleRender(RenderManager rm) {
        //TODO: add render code
        stateManager.render(rm);
    }
}
